package com.test.model.rest;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.test.constant.ErrCode;
import com.test.tool.StringTool;
import lombok.Data;

/**
 * Created by andy on 6/9/15.
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestResponse<T> {

    protected int status;
    protected String message;
    protected T data;
    protected long createdAt;
    protected long updatedAt;

    public RestResponse() {
        this(ErrCode.OK);
    }

    public RestResponse(ErrCode status) {
        this(status, status.info());
    }

    public RestResponse(ErrCode status, String message) {
        this.status = status.code();
        this.message = (message == null) ? status.info() : message;
        this.createdAt = System.currentTimeMillis() / 1000;
        this.updatedAt = this.createdAt;
    }

    public RestResponse(ErrCode status, String format, Object... arguments) {
        this(status, StringTool.formatString(format, arguments));
    }

    public RestResponse<T> data(T value) {
        data = value;
        return this;
    }
}
